#pragma once

#include <iostream>
#include <assert.h>

namespace My_List
{
	template<class T>
	struct list_node
	{
		list_node* _next;
		list_node* _prev;
		T _data;

		list_node(const T& x = T())
			:_next(nullptr)
			,_prev(nullptr)
			,_data(x)
		{}

	};

	template <class T,class Ref,class Ptr>
	struct list_iterator
	{
		typedef list_node<T> Node;
		typedef list_iterator<T, Ref, Ptr> Self;
		Node* _node;

		list_iterator(Node* node)
			:_node(node)
		{}

		Ref operator*()
		{
			return _node->_data;
		}

		Ptr operator->()
		{
			return &_node->_data;
		}

		Self& operator++()
		{
			_node = _node->_next;
			return *this;
		}

		Self operator++(int)
		{
			Self tmp(*this);
			_node = _node->_next;
			return tmp;
		}

		Self& operator--()
		{
			_node = _node->_prev;
			return *this;
		}

		Self operator--(int)
		{
			Self tmp(*this);
			_node = _node->_prev;
			return tmp;
		}

		bool operator!=(const Self& s)
		{
			return _node != s._node;
		}

		bool operator==(const Self& s)
		{
			return _node == s._node;
		}



	};

	template<class T>
	class list
	{
		typedef list_node<T> Node;
	public:

		typedef list_iterator<T, T&, T*> iterator;
		typedef list_iterator<T, const T&, const T*> const_iterator;

		iterator begin()
		{
			return _head->_next;
		}

		iterator end()
		{
			return _head;
		}

		const_iterator begin() const
		{
			return _head->_next;
		}

		const_iterator end() const
		{
			return _head;
		}

		

		void empty_init()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;

			_size = 0;
		}

		list()
		{
			empty_init();
		}

		list(std::initializer_list<T> il)
		{
			empty_init();
			for (auto e : il)
			{
				push_back(e);
			}
		}

		list(const list<T>& lt)
		{
			empty_init();
			for (auto e : lt)
			{
				push_back(e);
			}
		}

		void swap(list<T> lt)
		{
			std::swap(_head, lt._head);
			std::swap(_size, lt._size);
		}

		list<T>& operator=(list<T> lt)
		{
			swap(lt);
			return *this;
		}

		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}

		void clear()
		{
			auto it = begin();
			while (it != end())
			{
				it = erase(it);
			}
		}

		size_t size()
		{
			return _size;
		}

		void push_back(const T& x)
		{
			//Node* tail = _head->_prev;
			//Node* newnode = new Node(x);
			//newnode->_prev = tail;
			//newnode->_next = _head;
			//tail->_next = newnode;
			//_head->_prev = newnode;

			insert(end(), x);
			//++_size;
		}

		void push_front(const T& x)
		{
			insert(begin(), x);
		}

		void pop_back()
		{
			erase(--end());
		}

		void pop_front()
		{
			erase(begin());
		}

		void insert(iterator pos,const T& x)
		{
			Node* pcur = pos._node;
			Node* prev = pcur->_prev;
			Node* newnode = new Node(x);

			newnode->_next = pcur;
			newnode->_prev = prev;
			pcur->_prev = newnode;
			prev->_next = newnode;

			++_size;
		}

		iterator erase(iterator pos)
		{
			assert(pos != end());
			Node* pcur = pos._node;
			Node* next = pcur->_next;
			Node* prev = pcur->_prev;

			prev->_next = next;
			next->_prev = prev;

			delete pcur;
			--_size;
			return next;
		}

	private:
		Node* _head;
		size_t _size;
	};

	void test01()
	{
		list<int> lt1;
		lt1.push_back(1);
		lt1.push_back(2);
		lt1.push_back(3);
		lt1.push_back(4);

		lt1.push_front(10);
		lt1.push_front(100);
		lt1.push_front(1000);
		lt1.push_front(10000);
		
		for (auto e : lt1)
		{
			std::cout << e << " ";
		}
		std::cout << std::endl;

		std::cout << "Size: " << lt1.size() << std::endl;

		lt1.pop_back();
		lt1.pop_front();

		list<int>::iterator it = lt1.begin();
		while (it != lt1.end())
		{
			//*it *= 10;
			std::cout << *it << " ";
			it++;
		}
		std::cout << std::endl;

		std::cout << "Size: " << lt1.size() << std::endl;
	}

	void test02()
	{
		list<int> lt1;
		lt1.insert(lt1.begin(), 10);
		lt1.insert(lt1.begin(), 20);
		lt1.insert(lt1.begin(), 30);
		lt1.insert(lt1.begin(), 40);
		lt1.insert(lt1.begin(), 50);

		list<int>::iterator it = lt1.begin();
		while (it != lt1.end())
		{
			std::cout << *it << " ";
			it++;
		}
		std::cout << std::endl;
	}

	void test03()
	{
		list<int> lt1({ 1,2,3,4,5,6 });
		for (auto e : lt1)
		{
			std::cout << e << " ";
		}
		std::cout << std::endl;

		list<int> lt2(lt1);
		for (auto e : lt2)
		{
			std::cout << e << " ";
		}
		std::cout << std::endl;

		list<int> lt3({ 6,5,4,3,2,1 });
		for (auto e : lt3)
		{
			std::cout << e << " ";
		}
		std::cout << std::endl;

		lt2 = lt3;
		for (auto e : lt2)
		{
			std::cout << e << " ";
		}
		std::cout << std::endl;
	}
}